home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / SCREEN / CURSES01 / minix / c / cursesio < prev    next >
Text File  |  1993-08-11  |  10KB  |  383 lines

  1. #include <curses.h>
  2. #include "curspriv.h"
  3. #include <termcap.h>
  4. /************************************************************************
  5.  *                                                                      *
  6.  *                       Tiny pseudo "curses" package                   *
  7.  *                                                                      *
  8.  *              v1.0    870117  DBW - D. Wecker, initial hack           *
  9.  *              v1.1    881003  W. Toomey, hacked to get it to use      *
  10.  *                              Termcap, and to not foul input.         *
  11.  *                              Borrowed a bit of code from Alistair    *
  12.  *                              Crooks' `show.c' posted on 870730.      *
  13.  *              v1.2    900906  D. Cope beefed up to close to full      *
  14.  *                              curses support                          *
  15.  *              v1.3    900920  M. Haardt removed some little bugs,     *
  16.  *                              added block grafic characters           *
  17.  ************************************************************************/
  18.  
  19. #ifdef TERMIO
  20. #include <termio.h>
  21. #else
  22. #include <sgtty.h>
  23. #endif
  24. #include <stdio.h>
  25. #include <signal.h>
  26.  
  27. #ifdef TERMIO
  28. struct termio old_tty, new_tty;
  29. #else
  30. struct sgttyb   old_tty, new_tty;
  31. #endif
  32.  
  33. extern char *tgetstr();         /* termcap getstring capability */
  34. extern char *tgoto();           /* termcap goto (x, y) */
  35. void scrncolor();               /* forward declarition */
  36.  
  37. extern short ospeed = 5;        /* for UNIX Lib */
  38.  
  39. int  lastcolor = 0x700;         /* last color received fst is white on black */
  40. int  lastattr  = 0;             /* last attribute received */
  41. int  curmod = 1;                /* cursor initially set to on */
  42. char termcap[1024];             /* termcap buffer */
  43. char tc[200];                   /* area to hold string capabilities */
  44. char *ttytype;                  /* terminal type from env */
  45. char *arp;                      /* pointer for use in tgetstr */
  46. char *cp;                       /* character pointer */
  47.  
  48. char *cl;                       /* clear screen capability */
  49. char *cm;                       /* cursor motion capability */
  50. char *so;                       /* start standout capability */
  51. char *se;                       /* end standout capability */
  52. char *mr;                       /* start of reverse */
  53. char *me;                       /* revert to normal */
  54. char *mb;                       /* start of blink */
  55. char *md;                       /* start of bold */
  56. char *us;                       /* start of underscore */
  57. char *ue;                       /* end of underscore */
  58. char *vi;                       /* cursor invisible */
  59. char *ve;                       /* cursor normal */
  60. char *as;                       /* alternative charset start */
  61. char *ae;                       /* alternative charset end */
  62.  
  63. /*
  64.  *      fatal - report error and die. Never returns
  65.  */
  66. void fatal(s)
  67. char *s;
  68. {
  69.   (void) fprintf(stderr, "curses: %s\n", s);
  70.   exit(1);
  71. }
  72.  
  73.  
  74. /*
  75.  *      outc - call putchar, necessary because putchar is a macro.
  76.  */
  77. void outc(c)
  78. int c;
  79. {
  80.   putchar(c);
  81. }
  82.  
  83.  
  84. /* get cbreak mode  */
  85. int gcb()
  86. {
  87. #ifdef TERMIO
  88.         ioctl(0,TCGETA,&old_tty);
  89.         return(old_tty.c_lflag & ICANON? FALSE : TRUE);
  90. #else
  91.         ioctl(0,TIOCGETP,&old_tty);     /* get and return old setting */
  92.         return(old_tty.sg_flags & CBREAK? TRUE : FALSE);
  93. #endif
  94. }
  95.  
  96. /* set cbreak mode  */
  97. void scb(s)
  98. int s;
  99. {
  100. #ifdef TERMIO
  101.         ioctl(0,TCGETA,&old_tty);
  102.         if(s)   
  103.                 {old_tty.c_lflag &= ~(ICANON|ECHO);
  104.                  old_tty.c_cc[VMIN] = 1;
  105.                  old_tty.c_cc[VTIME] = 0;
  106.                 }
  107.         else    
  108.                 {old_tty.c_lflag |= ICANON|ECHO;
  109.                  old_tty.c_cc[VEOF] = CEOF;
  110.                  old_tty.c_cc[VEOL] = CEOL;
  111.                 }
  112.         ioctl(0,TCSETA,&old_tty);
  113. #else
  114.         ioctl(0,TIOCGETP,&old_tty);     /* set or clear cbreak */
  115.         if(s)   
  116.                 {old_tty.sg_flags |= CBREAK;
  117.                  old_tty.sg_flags &= ~ECHO;
  118.                 }
  119.         else    
  120.                 {old_tty.sg_flags &= ~CBREAK;
  121.                  old_tty.sg_flags |= ECHO;
  122.                 }
  123.         ioctl(0,TIOCSETP,&old_tty);
  124. #endif
  125. }
  126.  
  127. /* get fixed lines */
  128. int glines()
  129. {
  130.         return(tgetnum("li")); 
  131. }
  132.  
  133. /* get fixed cols */
  134. int gcols()
  135. {
  136.         return(tgetnum("co")); 
  137. }
  138.  
  139. /* output char with attribute */
  140. void _cursescattr(a, b, c, d)
  141. int a, b, c, d;
  142. {
  143.         if(lastattr != (c & 0xff))      /* attribute first */
  144.         {
  145.                 c = c & 0xff;
  146.                 lastattr = c;
  147.                 setdef();
  148.                 if ((c << 8) & A_ALTCHARSET)
  149.                         if (as) tputs(as, 1, outc);
  150.                 if ((c << 8) & A_BLINK)
  151.                         tputs(mb, 1, outc);
  152.                 if ((c << 8) & A_BOLD)
  153.                         tputs(md, 1, outc);
  154.                 if ((c << 8) & A_REVERSE)
  155.                         tputs(mr, 1, outc);
  156.                 if ((c << 8) & A_STANDOUT)
  157.                         tputs(so, 1, outc);
  158.                 if ((c << 8) & A_UNDERLINE)
  159.                         tputs(us, 1, outc);
  160.                 c = lastcolor;
  161.                 lastcolor = 0;
  162.                 scrncolor(c);
  163.         }
  164.         outc(b);                /* then out the character */
  165. }
  166.  
  167. /* set the screen colors  */
  168. void scrncolor(colors)
  169. int colors;
  170. {
  171.         int e, f;
  172.         if(lastcolor != colors)
  173.         {
  174.                 lastcolor = colors;
  175.                 e = colors & 0x700;
  176. #ifdef ARCH
  177.                 f = colors & 7;
  178.         printf("\021%c\021%c", e>>8, f+128);
  179. #else
  180.                 e = (e >> 8) + 30;
  181.                 f = colors & 7;
  182.                 f = f  + 40;
  183.                 printf("\033[%d;%dm", e, f);
  184. #endif
  185.         }
  186. }
  187.  
  188. /* How do we do this with the cursor */
  189. int gcmode()
  190. {
  191.         return(curmod);         /* condition of the cursor */
  192. }
  193.  
  194. void cmode(a)
  195. int a;
  196. {
  197.         if (a && ve !=NULL) tputs(ve,1,outc);
  198.         else if (vi!=NULL) tputs(vi,1,outc);
  199.         curmod = a;
  200. }
  201.  
  202. /* move cursor to r,c */
  203. void poscur(d, r, c)
  204. int d, r, c;
  205. {
  206.         tputs(tgoto(cm, c, r),1,outc);
  207.  
  208. }
  209.  
  210. /* this is get key */
  211. int getkey()
  212. {
  213.  
  214.         return(getchar());
  215. }
  216.  
  217. int getkeytst()
  218. {
  219.         return(kbhit());
  220. }
  221.  
  222. /*  flush all input away */
  223. void kbflush()
  224. {
  225. #ifdef TERMIO
  226.         ioctl(0, TCFLSH,0);
  227. #else
  228.         ioctl(0, TIOCFLUSH,0);
  229. #endif
  230. }
  231.  
  232. /*  key board been hit */
  233. int kbhit()
  234. {
  235.         int n = 0;
  236.         return (TRUE);
  237. }
  238.  
  239.  
  240. /* clear the screen */
  241. void clrscr()
  242. {
  243.         tputs(cl, 1, outc);
  244. }
  245.  
  246. /* this are terminal independent characters which can be used in curses */
  247.  
  248. unsigned char ACS_ULCORNER;
  249. unsigned char ACS_LLCORNER;
  250. unsigned char ACS_URCORNER;
  251. unsigned char ACS_LRCORNER;
  252. unsigned char ACS_RTEE;
  253. unsigned char ACS_LTEE;
  254. unsigned char ACS_BTEE;
  255. unsigned char ACS_TTEE;
  256. unsigned char ACS_HLINE;
  257. unsigned char ACS_VLINE;
  258. unsigned char ACS_PLUS;
  259. unsigned char ACS_S1;
  260. unsigned char ACS_S9;
  261. unsigned char ACS_DIAMOND;
  262. unsigned char ACS_CKBOARD;
  263. unsigned char ACS_DEGREE;
  264. unsigned char ACS_PLMINUS;
  265. unsigned char ACS_BULLET;
  266. unsigned char ACS_LARROW;
  267. unsigned char ACS_RARROW;
  268. unsigned char ACS_DARROW;
  269. unsigned char ACS_UARROW;
  270. unsigned char ACS_BOARD;
  271. unsigned char ACS_LANTERN;
  272. unsigned char ACS_BLOCK;
  273.  
  274. /* these defines describe the full set of grafic block characters which
  275.    can be defined via termcap. */
  276.  
  277. #define RIGHTARROW  0
  278. #define LEFTARROW   1
  279. #define DOWNARROW   2
  280. #define UPARROW     3
  281. #define FULLSQUARE  4
  282. #define GREYSQUARE  5
  283. #define EMPTYSQUARE 6
  284. #define LATERN      7
  285. #define DIAMOND     8
  286. #define DEGREE      9
  287. #define PLUSMINUS  10
  288. #define DOWNRIGHT  11
  289. #define UPRIGHT    12
  290. #define UPLEFT     13
  291. #define DOWNLEFT   14
  292. #define CROSS      15
  293. #define UPLINE     16
  294. #define UPMIDLINE  17
  295. #define MIDLINE    18
  296. #define DOMIDLINE  19
  297. #define DOWNLINE   20
  298. #define TEELEFT    21
  299. #define TEERIGHT   22
  300. #define TEEHEAD    23
  301. #define TEENORMAL  24
  302. #define VERTLINE   25
  303. #define PARAGRAPH  26
  304.  
  305. #ifdef ARCH
  306. char _cursgraftable[28] = "><v^#+ #+'#+++++- - _++++| ";
  307. char _cursident[28]     = "+,.-0ahI`fgjklmnopqrstuvwx~";
  308. #else
  309. char _cursgraftable[28] = "><v^#+ #+'#+++++- \300 _++++| ";
  310. char _cursident[28]     = "+,.-0ahI`fgjklmnopqrstuvwx~";
  311. #endif
  312.  
  313. _curstcap()
  314. {
  315.   char *ac;
  316.   int i;
  317.  
  318.  if ((ttytype = getenv("TERM")) == (char *)NULL)
  319.         fatal("No terminal type set in environment");
  320.  
  321.   if (tgetent(termcap, ttytype) != 1) fatal("No termcap entry for terminal");
  322.   arp = tc;
  323.   cl = tgetstr("cl", &arp);
  324.   so = tgetstr("so", &arp);
  325.   se = tgetstr("se", &arp);
  326.   cm = tgetstr("cm", &arp);
  327.   mr = tgetstr("mr", &arp);
  328.   me = tgetstr("me", &arp);
  329.   mb = tgetstr("mb", &arp);
  330.   md = tgetstr("md", &arp);
  331.   us = tgetstr("us", &arp);
  332.   ue = tgetstr("ue", &arp);
  333.   vi = tgetstr("vi", &arp);
  334.   ve = tgetstr("ve", &arp);
  335.   as = tgetstr("as", &arp);
  336.   ae = tgetstr("ae", &arp);
  337.   ac = tgetstr("ac", &arp);
  338.  
  339.   if (ac)
  340.     while (*ac)
  341.     {
  342.       i=0;
  343.       while (*ac != _cursident[i]) i++;
  344.       _cursgraftable[i] = *++ac;
  345.       ac++;
  346.     }
  347.  
  348.   ACS_ULCORNER = _cursgraftable[UPLEFT];
  349.   ACS_LLCORNER = _cursgraftable[DOWNLEFT];
  350.   ACS_URCORNER = _cursgraftable[UPRIGHT];
  351.   ACS_LRCORNER = _cursgraftable[DOWNRIGHT];
  352.   ACS_RTEE = _cursgraftable[TEERIGHT];
  353.   ACS_LTEE = _cursgraftable[TEELEFT];
  354.   ACS_BTEE = _cursgraftable[TEEHEAD];
  355.   ACS_TTEE = _cursgraftable[TEENORMAL];
  356.   ACS_HLINE = _cursgraftable[MIDLINE];
  357.   ACS_VLINE = _cursgraftable[VERTLINE];
  358.   ACS_PLUS = _cursgraftable[PLUSMINUS];
  359.   ACS_S1 = _cursgraftable[UPLINE];
  360.   ACS_S9 = _cursgraftable[DOWNLINE];
  361.   ACS_DIAMOND = _cursgraftable[DIAMOND];
  362.   ACS_CKBOARD = _cursgraftable[GREYSQUARE];
  363.   ACS_DEGREE = _cursgraftable[DEGREE];
  364.   ACS_PLMINUS = _cursgraftable[PLUSMINUS];
  365.   ACS_BULLET = 'o'; /* where the hell is a bullet defined in termcap ??? */
  366.   ACS_LARROW = _cursgraftable[LEFTARROW];
  367.   ACS_RARROW = _cursgraftable[RIGHTARROW];
  368.   ACS_DARROW = _cursgraftable[DOWNARROW];
  369.   ACS_UARROW = _cursgraftable[UPARROW];
  370.   ACS_BOARD = _cursgraftable[EMPTYSQUARE];
  371.   ACS_LANTERN = _cursgraftable[LATERN];
  372.   ACS_BLOCK = _cursgraftable[FULLSQUARE];
  373.   /* wow, I got it! */  
  374.  
  375.   tputs(cl, 1, outc);  
  376. }
  377.  
  378. void setdef()           /* set screen back to default settings */
  379. {
  380.         tputs(me, 1, outc);
  381.         if (ae) tputs(ae, 1, outc);
  382. }
  383.